﻿using SAD_Assignment1.BLO;
using SAD_Assignment1.DTO;
using SAD_Assignment1.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAD_Assignment1.Public
{
    public partial class ViewPhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initComponent();
                int photoID = 0;
                try { photoID = Int32.Parse(Request.Params["ID"].ToString()); }
                catch (Exception) { }
                loadComment(photoID);
            }
        }

        protected string CmtCSS(bool isReply)
        {
            return isReply ? "comment reply" : "comment";
        }

        protected string CmdName(bool isReply)
        {
            return isReply ? "reply" : "comment";
        }


        protected bool DeleteVisible(int accID)
        {
            if (Session["account"] == null) return false;
            AccountDTO accDTO = (AccountDTO)Session["account"];
            string[] roles = new string[] {"admin","staff" };
            if (roles.Contains(accDTO.Roles)) return true;            
            return accDTO.ID == accID;
        }

        protected string GetAccountName(int id)
        {
            if (Session["account"] == null) { return null; }
            AccountDTO accDTO = (AccountDTO)Session["account"];
            return accDTO.Name;
        }

        private void initComponent()
        {            
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"].ToString()); }
            catch (Exception) { }
            if (photoID == 0) { Response.Redirect("~/Public/ErrorPage.aspx"); }
            PhotoBLO photoBLO = new PhotoBLO();
            var list = photoBLO.getPhoto(photoID);
            grid.DataSource = list;
            grid.DataBind();
            if (list.Count == 0)
            {
                Response.Redirect(Resource.Error);
            }
        }

        protected void sendCmtBtn_Command(object sender, CommandEventArgs e)
        {
            if (txtCmtBody.Text.Trim().Length == 0) return;
            CommentBLO cmtBLO = new CommentBLO();
            string email = ((AccountDTO)Session["account"]).Email;
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }
            cmtBLO.AddComment(photoID, txtCmtBody.Text, email);
            loadComment(photoID);
            txtCmtBody.Text = "";
        }

        private void loadComment(int photoID)
        {
            CommentBLO cmtBLO = new CommentBLO();
            var cmtList = cmtBLO.getAllComment(photoID);
            cmtGrid.DataSource = cmtList;
            cmtGrid.DataBind();
            loginHL.NavigateUrl = Resource.Login + "?redirect=" + Resource.ViewPhoto + "?ID=" + Request.Params["ID"];
            if (Session["account"] != null)
            {
                loginRequire.Visible = false;
                cmtPanel.Visible = true;
                AccountDTO accDTO = (AccountDTO)Session["account"];
                userInfoHL.Text = accDTO.Name;
            }
            else
            {
                loginRequire.Visible = true;
                cmtPanel.Visible = false;
            }
        }

        protected void DeleteBtn_Command(object sender, CommandEventArgs e)
        {
            int cmtID = 0;
            try { cmtID = Int32.Parse((string)e.CommandArgument); }
            catch (Exception) { }
            CommentBLO cmtBLO = new CommentBLO();
            if (e.CommandName.Equals("comment"))
            {
                cmtBLO.DeleteComment(cmtID);
            }
            else if (e.CommandName.Equals("reply"))
            {
                cmtBLO.DeleteReply(cmtID);
            }

            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }
            loadComment(photoID);
        }

        protected void replyBtn_Command(object sender, CommandEventArgs e)
        {
            int index = Int32.Parse(e.CommandArgument.ToString());
            Panel replyPanel = (Panel)cmtGrid.Items[index].FindControl("replyPanel");
            replyPanel.Visible = true;
            if(Session["account"]!=null)
            {
                HyperLink userInfoHL2 = (HyperLink)cmtGrid.Items[index].FindControl("userInfoHL2");
                userInfoHL2.Text = ((AccountDTO)Session["account"]).Name;
            }
            TextBox txtReplyBody = (TextBox)cmtGrid.Items[index].FindControl("txtReplyBody");
            txtReplyBody.Focus();
        }

        protected void replyBtn_Load(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn == null) return;
            if (Session["account"] == null)
            {
                btn.Visible = false;
            }

        }

        protected void sendReplyBtn_Command(object sender, CommandEventArgs e)
        {
            int index = Int32.Parse(e.CommandArgument.ToString());
            HiddenField hiddenID = (HiddenField)cmtGrid.Items[index].FindControl("hiddenID");
            TextBox txtBody = (TextBox)cmtGrid.Items[index].FindControl("txtReplyBody");
            AccountDTO accDTO = (AccountDTO)Session["account"];
            CommentBLO cmtBLO = new CommentBLO();
            cmtBLO.AddReply(Int32.Parse(hiddenID.Value), txtBody.Text, accDTO.Email);
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }            
            loadComment(photoID);
            txtBody.Text = "";
        }

        protected void LikeBtn_Command(object sender, CommandEventArgs e)
        {
            int photoID = 0;
            try { photoID = Int32.Parse(e.CommandArgument.ToString()); }
            catch (Exception) { }
            if (Session["account"] == null) 
            {
                Response.Redirect(Resource.Login+"?redirect=~/Public/ViewPhoto.aspx?ID="+photoID);
                return;
            }                        
            AccountDTO accDTO = (AccountDTO)Session["account"];
            LikeBLO likeBLO = new LikeBLO();
            likeBLO.LikePhoto(photoID, accDTO.ID);            
            likeBtnGroup_Load(null);
        }

        protected void LikeBtn_Load(object sender, EventArgs e)
        {
            Button LikeBtn = (Button)sender;            
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }
            if (Session["account"] != null)
            {
                AccountDTO accDTO = (AccountDTO)Session["account"];
                LikeBLO likeBLO = new LikeBLO();
                if (likeBLO.IsLikePhoto(photoID, accDTO.ID))
                {
                    LikeBtn.CssClass = "btn btn-danger";
                    LikeBtn.Enabled = false;
                }
                else
                {
                    LikeBtn.CssClass = "btn btn-info";
                    LikeBtn.Enabled = true;
                }
            }
        }

        protected void likeCountLB_Load(object sender, EventArgs e)
        {
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }
            LikeBLO likeBLO = new LikeBLO();
            Label likeCountLB = (Label)sender;
            likeCountLB.Text = likeBLO.countLikePhoto(photoID).ToString();
        }

        protected void UnlikeBtn_Command(object sender, CommandEventArgs e)
        {
            int photoID = 0;
            try { photoID = Int32.Parse(e.CommandArgument.ToString()); }
            catch (Exception) { }
            if (Session["account"] == null)
            {
                Response.Redirect(Resource.Login + "?redirect=~/Public/ViewPhoto.aspx?ID=" + photoID);
                return;
            }  
            AccountDTO accDTO = (AccountDTO)Session["account"];
            LikeBLO likeBLO = new LikeBLO();
            likeBLO.UnlikePhoto(photoID, accDTO.ID);
            //Button UnlikeBtn = (Button)sender;
            //UnlikeBtn_Load(UnlikeBtn, null);
            //Label unlikeCountLB = (Label)grid.Items[0].FindControl("unlikeCountLB");
            //unlikeCountLB_Load(unlikeCountLB, null);
            likeBtnGroup_Load(null);
        }

        protected void UnlikeBtn_Load(object sender, EventArgs e)
        {
            Button unlikeBtn = (Button)sender;
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }
            if (Session["account"] != null)
            {
                AccountDTO accDTO = (AccountDTO)Session["account"];
                LikeBLO likeBLO = new LikeBLO();
                if (likeBLO.IsUnlikePhoto(photoID, accDTO.ID))
                {
                    unlikeBtn.CssClass = "btn btn-danger";
                    unlikeBtn.Enabled = false;
                }
                else
                {
                    unlikeBtn.CssClass = "btn btn-info";
                    unlikeBtn.Enabled = true;
                }
            }
        }

        protected void unlikeCountLB_Load(object sender, EventArgs e)
        {
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"]); }
            catch (Exception) { }
            LikeBLO likeBLO = new LikeBLO();
            Label unlikeCountLB = (Label)sender;
            unlikeCountLB.Text = likeBLO.countUnlikePhoto(photoID).ToString();
        }

        private void likeBtnGroup_Load(EventArgs e)
        {
            //like
            Button LikeBtn = (Button)grid.Items[0].FindControl("LikeBtn");
            LikeBtn_Load(LikeBtn, e);
            Label likeCountLB = (Label)grid.Items[0].FindControl("likeCountLB");
            likeCountLB_Load(likeCountLB, e);
            //unlike
            Button UnlikeBtn = (Button)grid.Items[0].FindControl("UnlikeBtn"); ;
            UnlikeBtn_Load(UnlikeBtn, e);
            Label unlikeCountLB = (Label)grid.Items[0].FindControl("unlikeCountLB");
            unlikeCountLB_Load(unlikeCountLB, e);
        }

        protected void ApproveBtn_Command(object sender, CommandEventArgs e)
        {
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"].ToString()); }
            catch (Exception) { }
            PhotoBLO photoBLO = new PhotoBLO();
            photoBLO.ApprovePhoto(photoID);
            Response.Redirect(Resource.Home);
        }

        protected void ApproveBtn_Load(object sender, EventArgs e)
        {
            Button ApproveBtn = (Button)sender;
            HiddenField hiddenPhotoID = (HiddenField) ApproveBtn.Parent.FindControl("hiddenPhotoID");
            if (Session["account"] != null)
            {
                AccountDTO accDTO = (AccountDTO)Session["account"];
                string[] roles = new string[] { "admin", "staff" };
                if (roles.Contains(accDTO.Roles) && hiddenPhotoID.Value.Equals("vote"))
                {
                    ApproveBtn.Visible = true;
                }
                else
                {
                    ApproveBtn.Visible = false;
                }
            }
        }

        protected void DeletePhotoBtn_Load(object sender, EventArgs e)
        {
            Button ApproveBtn = (Button)sender;
            if (Session["account"] != null)
            {
                AccountDTO accDTO = (AccountDTO)Session["account"];
                string[] roles = new string[] { "admin", "staff" };
                if (roles.Contains(accDTO.Roles))
                {
                    ApproveBtn.Visible = true;
                }
                else
                {
                    ApproveBtn.Visible = false;
                }
            }
        }

        protected void DeletePhotoBtn_Command(object sender, CommandEventArgs e)
        {
            int photoID = 0;
            try { photoID = Int32.Parse(Request.Params["ID"].ToString()); }
            catch (Exception) { }
            string url = e.CommandArgument.ToString();
            PhotoBLO photoBLO = new PhotoBLO();
            bool success = photoBLO.DeletePhoto(photoID);
            if (success)
            {
                string realPath = Server.MapPath(url);
                System.IO.File.Delete(realPath);
            }
            Response.Redirect(Resource.Home);
        }
    }
}